package main.java.tmi.clearance.web;

import java.sql.SQLException;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import main.java.tmi.common.service.UserVO;
import main.java.tmi.clearance.service.ClearanceService;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;


/**
 *  Controller
 *
 * @author min
 * @since 2010. 09. 08.
 */
@Controller
public class ClearanceController {
	
    @Resource(name = "clearanceServiceImpl")
    private ClearanceService clearanceService;
    
    /**
     * The Constructor
     */
    public ClearanceController() {
        //logger = LogFactory.getLog(this.getClass());
    }

    /**
     * 
     * 
     * @param HttpServletResponse response
     * @param ModelMap model
     * @return
     * @throws SQLException
     */
    @RequestMapping("/clearance")
    public String viewList(HttpServletRequest request, HttpServletResponse response,
    		@RequestParam Map param, ModelMap model) throws SQLException {
        System.out.println("====================================================");
        System.out.println("===============clearance start=========================");
        System.out.println("====================================================");
        UserVO userInfo = (UserVO) RequestContextHolder.getRequestAttributes().getAttribute("userInfo", RequestAttributes.SCOPE_SESSION);
		model.addAttribute("roleId",userInfo.getRoleId());
        model.addAttribute("ordPseq",param.get("ordPseq"));
        model.addAttribute("ordNo1",param.get("ordNo1"));
        model.addAttribute("ordNo2",param.get("ordNo2"));
        model.addAttribute("clearNo",param.get("clearNo"));
        model.addAttribute("mawb",param.get("mawb"));
        model.addAttribute("hawb",param.get("hawb"));
        return "/tmi/clearance/clearance";
    } 
     

    /**
     * 조회
     */
    @RequestMapping("/getClearanceView")
    public void getClearanceView(@RequestParam Map param, Model model) throws Exception {
        System.out.println("=======================> getClearanceView start");

        //저장 완료 후 조회
		model.addAttribute("resultMap",clearanceService.getClearanceView(param));
		model.addAttribute("resultDetailList",clearanceService.getClearanceDetailList(param));
    }
    

    /**
     * clearance SAVE
     */
    @RequestMapping("/saveClearance")
    public void saveClearance(@RequestParam Map param, Model model , HttpServletRequest request) throws Exception {
    	System.out.println("=======================> saveClearance Start");
    	
    	UserVO userInfo = (UserVO) RequestContextHolder.getRequestAttributes().getAttribute("userInfo", RequestAttributes.SCOPE_SESSION);
    	param.put("sesseionId", userInfo.getUserId());
    	System.out.println("sesseionId============> "+userInfo.getUserId());
    	
    	String saveStatus = param.get("saveStatus").toString();
    	System.out.println("=======================> saveStatus"+saveStatus);
    	
    	if(saveStatus.equals("SAVE")){
    		clearanceService.addClearance(param , request);
    	}else if(saveStatus.equals("UPDATE")){
        	model.addAttribute("result", clearanceService.modifyClearance(param , request) );
    	}

        //저장 완료 후 조회
    	model.addAttribute("resultMap",clearanceService.getClearanceView(param));
		model.addAttribute("resultDetailList",clearanceService.getClearanceDetailList(param));
		model.addAttribute("result",true);
    }
    
    
    /**
    * 삭제
    */
    @RequestMapping("/clearanceDetailUpdate")
    public void clearanceDetailUpdate(@RequestParam Map param, Model model) throws Exception {
        System.out.println("=======================> clearanceDetailUpdate Start");
    	UserVO userInfo = (UserVO) RequestContextHolder.getRequestAttributes().getAttribute("userInfo", RequestAttributes.SCOPE_SESSION);
    	param.put("sesseionId", userInfo.getUserId());
    	
        model.addAttribute("result", clearanceService.clearanceDetailUpdate(param) );
    }
    
    
    /**
     * 확정
     */
     @RequestMapping("/confirmPacking")
     public void confirmPacking(@RequestParam Map param, Model model) throws Exception {
         System.out.println("=======================> confirmPacking Start");
         
     	UserVO userInfo = (UserVO) RequestContextHolder.getRequestAttributes().getAttribute("userInfo", RequestAttributes.SCOPE_SESSION);
     	param.put("sesseionId", userInfo.getUserId());
     	
         model.addAttribute("result", clearanceService.confirmPacking(param) );
     }
    
     

     @RequestMapping("/deleteDoClearance")
     public void deleteDoClearance(@RequestParam Map param, Model model , HttpServletRequest request) throws Exception {
     	System.out.println("=======================> deleteDoClearance Start");
     	
    	UserVO userInfo = (UserVO) RequestContextHolder.getRequestAttributes().getAttribute("userInfo", RequestAttributes.SCOPE_SESSION);
    	param.put("sesseionId", userInfo.getUserId());
    	
 		model.addAttribute("result",clearanceService.deleteDo(param));
     }
     
     
    
}
